package com.leyou.trade.utils;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.local.UserContext;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Data
@Component
public class CollectionNameBuilder {

    @Value("${ly.mongo.collectionNamePrefix}")
    private String collectionNamePrefix;

    public String calculate(){
        // 获取用户id
        UserDetails user = UserContext.getUser();
        if (user == null) {
            return "";
        }
        // 计算hash值
        int index = user.getId().hashCode() % 100;
        // 得到集合名称
        return collectionNamePrefix + index;
    }
}
